package com.study.view;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

import com.study.service.ServiceLogin;
import com.study.service.UserInfoBean;

import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {

    Button onLogin;
    ServiceLogin login;
    private static final String TAG = "LoginActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        onLogin = findViewById(R.id.onLogin);
        onLogin.setOnClickListener(view->{
            Intent intent = new Intent();
            intent.setAction("com.study.loginSerivce");
            intent.setPackage("com.study.service");
            bindService(intent,conn,BIND_AUTO_CREATE);
        });
    }


    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

            login = ServiceLogin.Stub.asInterface(service);
            try {
                UserInfoBean userInfoBean = login.onLogin();
                Log.d(TAG, "onServiceConnected: ===================================================="+userInfoBean.userName);
            } catch (RemoteException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "onServiceDisconnected: "+login);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }
}
